package com.wlyuan.open.scanner.domain.order;

import com.alibaba.excel.util.StringUtils;
import com.wlyuan.open.scanner.constant.Constants;
import com.wlyuan.open.scanner.dao.entity.OrderGoodsDO;
import com.wlyuan.open.scanner.utils.mappings.MappingInvalid;
import lombok.var;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;
import org.mapstruct.factory.Mappers;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author lijing
 * @description
 */
public class OrderGoodsConvert {

    private static final Convert CONVERT = Mappers.getMapper(Convert.class);

    @Mapper(uses = MappingInvalid.class)
    public interface Convert {

        @Mappings({
                @Mapping(target = "id", source = "goodslstId"),
                @Mapping(target = "goodsId", source = "goodslstGoodsid"),
                @Mapping(target = "orderId", source = "goodslstOrderid"),
                @Mapping(target = "materialCode", source = "goodslstBarcode"),
                @Mapping(target = "goodsName", source = "goodslstName"),
                @Mapping(target = "goodsTypeId", source = "goodsTypeId"),
                @Mapping(target = "goodsTypeName", source = "goodsTypeName"),
                @Mapping(target = "spec", source = "goodslstSpc"),
                @Mapping(target = "weight", source = "goodslstSingleweight"),
                @Mapping(target = "volume", source = "goodslstSinglevolume"),
                @Mapping(target = "value", source = "goodslstSingleprice"),
                @Mapping(target = "batchNo", source = "goodslstBatchno"),
                @Mapping(target = "quantity", source = "goodslstQty"),
                @Mapping(target = "desc", source = "goodslstComments"),
                @Mapping(target = "createAt", source = "goodslstCreator"),
                @Mapping(target = "createTime", source = "goodslstInserttime"),
                @Mapping(target = "updateAt", source = "goodslstUpdater"),
                @Mapping(target = "updateTime", source = "goodslstUpdatetime"),
                @Mapping(target = "priceUnit", source = "goodslstUnit"),

                @Mapping(target = "goodsSign.materialCode", source = "goodslstBarcode"),
                @Mapping(target = "goodsSign.batchNo", source = "goodslstBatchno"),
                @Mapping(target = "goodsSign.quantity", source = "goodslstReceiptqty"),
                @Mapping(target = "goodsSign.exceptionQuantity", source = "goodslstExceptionqty"),
                @Mapping(target = "goodsSign.lackQuantity", source = "goodslstLackqty"),
                @Mapping(target = "goodsSign.loseQuantity", source = "goodslstLoseqty"),
                @Mapping(target = "goodsSign.damageQuantity", source = "goodslstDamageqty"),
                @Mapping(target = "goodsSign.otherQuantity", source = "goodslstOtherqty"),
                @Mapping(target = "goodsSign.exceptionDesc", source = "goodslstExceptiondesc"),
                @Mapping(target = "goodsSign.goodsName", source = "goodslstName"),
                @Mapping(target = "goodsSign.goodsTypeId", source = "goodsTypeId"),
                @Mapping(target = "goodsSign.goodsTypeName", source = "goodsTypeName"),
                @Mapping(target = "goodsSign.spec", source = "goodslstSpc"),
                @Mapping(target = "goodsSign.weight", source = "goodslstSingleweight"),
                @Mapping(target = "goodsSign.volume", source = "goodslstSinglevolume"),
                @Mapping(target = "goodsSign.value", source = "goodslstSingleprice"),
                @Mapping(target = "goodsSign.unit", source = "unit"),

                @Mapping(target = "goodsReceipt.materialCode", source = "goodslstBarcode"),
                @Mapping(target = "goodsReceipt.batchNo", source = "goodslstBatchno"),
                @Mapping(target = "goodsReceipt.quantity", source = "goodslstReceiptqtyV"),
                @Mapping(target = "goodsReceipt.exceptionQuantity", source = "goodslstExceptionqtyV"),
                @Mapping(target = "goodsReceipt.lackQuantity", source = "goodslstLackqtyV"),
                @Mapping(target = "goodsReceipt.loseQuantity", source = "goodslstLoseqtyV"),
                @Mapping(target = "goodsReceipt.damageQuantity", source = "goodslstDamageqtyV"),
                @Mapping(target = "goodsReceipt.otherQuantity", source = "goodslstOtherqtyV"),
                @Mapping(target = "goodsReceipt.exceptionDesc", source = "goodslstExceptiondescV"),
                @Mapping(target = "goodsReceipt.goodsName", source = "goodslstName"),
                @Mapping(target = "goodsReceipt.goodsTypeId", source = "goodsTypeId"),
                @Mapping(target = "goodsReceipt.goodsTypeName", source = "goodsTypeName"),
                @Mapping(target = "goodsReceipt.spec", source = "goodslstSpc"),
                @Mapping(target = "goodsReceipt.weight", source = "goodslstSingleweight"),
                @Mapping(target = "goodsReceipt.volume", source = "goodslstSinglevolume"),
                @Mapping(target = "goodsReceipt.value", source = "goodslstSingleprice"),
                @Mapping(target = "goodsReceipt.unit", source = "unit"),

                @Mapping(target = "invalid", source = "goodslstInvalid"),
                @Mapping(target = "comments", source = "goodslstComments"),
                @Mapping(target = "price", source = "goodslstPrice"),
                @Mapping(target = "notAxUnitPrice", source = "goodslstNotaxunitprice"),
                @Mapping(target = "notAxPrice", source = "goodslstNotaxprice"),
                @Mapping(target = "taxPrice", source = "goodslstTaxprice"),
                @Mapping(target = "totalPrice", source = "goodslstTotalprice"),
                @Mapping(target = "exceptionQtyTemp", source = "goodslstExceptionqtytemp"),
                @Mapping(target = "exceptionType", source = "goodslstExceptiontype"),
                @Mapping(target = "exceptionTypeV", source = "goodslstExceptiontypeV"),
                @Mapping(target = "noId", source = "goodslstNoid"),
                @Mapping(target = "refusedNo", source = "goodslstRefusedno"),
                @Mapping(target = "kbetrWs", source = "goodslsrKbetrWs"),
                @Mapping(target = "mwsbpZk", source = "goodslstMwsbpZk"),
                @Mapping(target = "mwsbp", source = "goodslstMwsbp"),
                @Mapping(target = "kbetr", source = "goodslstKbetr"),
                @Mapping(target = "exceptionDesc", source = "goodslstExceptiondesc"),
                @Mapping(target = "exceptionTag", source = "goodslstExceptiontag"),
                @Mapping(target = "code", source = "goodslstCode"),
                @Mapping(target = "singlePrice", source = "goodslstSingleprice"),
                @Mapping(target = "singleWeight", source = "goodslstSingleweight"),
                @Mapping(target = "singleVolume", source = "goodslstSinglevolume"),
                @Mapping(target = "factoryNo", source = "goodslstFactoryno"),
                @Mapping(target = "echo", source = "goodslstEcho"),
                @Mapping(target = "extendDoc", source = "goodslstExtenddoc1"),
        })
        OrderGoods convert(OrderGoodsDO source);

        List<OrderGoods> convert(List<OrderGoodsDO> list);

        OrderReceipt convertReceipt(OrderSign source);

        List<OrderReceipt> convertReceipt(List<OrderSign> source);

    }

    public static List<OrderGoods> convert(List<OrderGoodsDO> creation, Long tenantId) {
        if (CollectionUtils.isEmpty(creation)) {
            return Collections.emptyList();
        }
        return creation.stream().map(c -> convert(c, tenantId)).collect(Collectors.toList());
    }

    public static OrderGoods convert(OrderGoodsDO source, Long tenantId) {
        var convert = CONVERT.convert(source);
        Optional.ofNullable(convert.getEcho()).ifPresent(h -> {
            if (Constants.QZ_TENANT.equals(tenantId)) {
                h.put("shippingNo", StringUtils.isBlank(source.getGoodslstExtenddoc1()) ? "" : source.getGoodslstExtenddoc1());
            }
        });
        return convert;
    }

    public static List<OrderReceipt> convertReceipt(List<OrderSign> creation) {
        return CONVERT.convertReceipt(creation);
    }
}
